package cn.aresoft.framework.support.action;

import java.io.File;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.CookiesAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ValidationAware;
import com.opensymphony.xwork2.ValidationAwareSupport;


public abstract class BaseAction 
	extends ActionSupport 
		implements ServletRequestAware, ServletResponseAware, ServletContextAware, SessionAware, ValidationAware, CookiesAware{


	private static final long serialVersionUID = 7438837695341892720L;
	
	protected Map session;
	protected Map cookies;
	
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected HttpSession httpSession;
	
	protected ServletContext servletContext;
	protected ServletResponse servletResponse;

	public Map getSession() {
		return ActionContext.getContext().getSession();
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
	}

	public void setServletResponse(HttpServletResponse arg0) {
		response = arg0;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public ServletResponse getServletResponse() {
		return servletResponse;
	}

	public HttpSession getHttpSession() {
		return httpSession;
	}

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	public void setCookiesMap(Map cookies) {
		this.cookies = cookies;
	}
	
	//************* 验证支持 **************************************
	protected final ValidationAwareSupport validationAware = new ValidationAwareSupport();
	
	public void addActionError(String anErrorMessage){
		validationAware.addActionError(anErrorMessage);
	}
	
	public void addActionMessage(String aMessage){
		validationAware.addActionMessage(aMessage);
	}
	
	public void addFieldError(String fieldName,String errorMessage){
		validationAware.addFieldError(fieldName,errorMessage);
	}
	//***************************************************************
	
	//************* 获取Application, Servlet 上下文 *******************
	protected ApplicationContext getApplicationContext() {
		return WebApplicationContextUtils.getWebApplicationContext(ServletActionContext
				.getServletContext());
	}
	
	protected ServletContext getServletContext() {
		ActionContext ac = ActionContext.getContext();   
		ServletContext servletContext = (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT); 
		return servletContext;
	}
	
	//***************************************************************
	
	//************* 获取系统的路径方法 *********************************
	public String getBasePath() {
		String basePath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort() + getContextPath() + "/";
		return basePath;
	}
	
	public String getServletPath() {
		String servlet_path = request.getServletPath();
		return servlet_path;
	}

	public String getContextPath() {
		String context_path = request.getContextPath();
		return context_path;
	}

	public String getApplicationRealPath() {
		return this.request.getRealPath("/")+File.separator;
	}

	public String getApplicationRealPath(String fold) {
		return this.request.getRealPath(fold)+File.separator;
	}
	//***************************************************************

}
